//
//  ST_BTUpgradeController.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/21.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyAttributes

//全局只弹窗一次
var RecommendedUpgradeToast = false

var forceUpgradeToast = false

class ST_BTUpgradeController: UIViewController {

    
    static func upgradeNotification(text:String,forceUpgrade:Bool = false){
        
        if forceUpgrade{
            if forceUpgradeToast{
                return
            }
            forceUpgradeToast = true
        }else{
            if RecommendedUpgradeToast{
                return
            }
            RecommendedUpgradeToast = true
        }
        
        
        let vc = ST_BTUpgradeController()
        vc.overCurrentContext = true
        vc.forceUpgrade = forceUpgrade
        vc.notification_text = text
        GlobalPresent(toController: vc)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(340)
            make.height.equalTo(300)
            make.center.equalToSuperview()
        }
        
        root_view.addSubview(upgradeLabel)
        upgradeLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(33)
        }
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(35)
            make.right.equalToSuperview().offset(-35)
            make.top.equalTo(upgradeLabel.snp.bottom).offset(15)
        }
        
        
        root_view.addSubview(right_btn)
        right_btn.snp.makeConstraints { (make) in
            
            make.right.equalToSuperview().offset(-40)
            make.bottom.equalToSuperview().offset(-25)
            make.width.equalTo(120)
            make.height.equalTo(36)
            
        }
        
        root_view.addSubview(left_btn)
        left_btn.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview().offset(40)
            make.bottom.equalToSuperview().offset(-25)
            make.width.equalTo(120)
            make.height.equalTo(36)
            
        }
    }
    
    
    var forceUpgrade = false
    
    var notification_text = ""
    
    lazy var root_view:UIView = {
        let control = UIView()
        control.cornerRadius = 20
        control.backgroundColor = Color.white
        return control
    }()
    
    
    lazy var upgradeLabel:UILabel = {
        let control = UILabel.init()
        control.text = "推荐更新"
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.attributedText = self.notification_text.withAttributes([Attribute.textColor(Color.init(hexString: "#666666")!),.font(.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),.paragraphStyle(NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3))])
        control.numberOfLines = 0
        control.textAlignment = .left
//        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()

    lazy var right_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#42CEDA")
        control.cornerRadius = 18
        control.setTitleForAllStates("去下载")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.addTarget(self, action: #selector(goto_app_store), for: UIControl.Event.touchUpInside)
        return control
    }()
    
  
    lazy var left_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.cornerRadius = 18
        let leftTitle = forceUpgrade ? "关闭" : "取消"
        control.setTitleForAllStates(leftTitle)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        control.setTitleColor(UIColor.init(hexString: "#666666"), for: UIControl.State.normal)
        control.addTarget(self, action: #selector(cacnel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

//    MARK: - Event
    @objc func goto_app_store(){
        
        guard let url = URL.init(string: "https://apps.apple.com/cn/app/id1484898944")else{
            return
        }
        
        UIApplication.shared.openURL(url)
        
    }
    
    @objc func cacnel_handle(){
        
        if forceUpgrade{
            exit(0)
        }else{
            goback_previous_controller()
        }
        
    }
}
